package com.vxdata.utils.modules.utils.zhjt;

import org.apache.commons.codec.binary.Base64;

import java.io.*;

/**
 * file 与 base64 互转
 */
public class FileToBase64Util {
    public static String fileToBase64(String filePath) {
        return fileToBase64(new File(filePath));
    }


    public static String fileToBase64(File file) {
        String base64 = null;
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];
            in.read(bytes);
            base64 = new String(Base64.encodeBase64(bytes), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return base64;
    }

    public static void base64ToFile(String filePath, String base64) {
        String outFileName = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
        String outFilePath = filePath.substring(0, filePath.lastIndexOf(File.separator) + 1);
        base64ToFile(outFilePath, base64, outFileName);
    }


    public static void base64ToFile(String outFilePath, String base64, String outFileName) {
        File file = null;
        //创建文件目录
        String filePath = outFilePath;
        File dir = new File(filePath);
        if (!dir.exists() && !dir.isDirectory()) {
            dir.mkdirs();
        }
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        try {
            byte[] bytes = Base64.decodeBase64(base64);
            file = new File(filePath + File.separator + outFileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();

        //定义文件路径
        String filePath = "C:\\Users\\33067\\Desktop\\base64\\微信图片_20220916155525.png";
        //将文件转base64字符串
        String base64 = fileToBase64(filePath);

        System.out.println();
        //定义输出文件的路径outFilePath和输出文件名outoutFileName
        String outFilePath = "C:\\Users\\33067\\Desktop\\base64";
        String outFileName = "微信图片.png";
        //将BASE64解码成File文件
        base64ToFile(outFilePath, base64, outFileName);
        String filePath2 = "C:\\Users\\33067\\Desktop\\base642\\微信图片_20220916155525.png";
        base64ToFile(filePath2, base64);
        long endTime = System.currentTimeMillis();
        System.out.println(endTime - startTime);
    }
}
